import { ref } from 'vue'

/**
 * Toast工具类
 * @description 提供类似wx.showToast的API，使用自定义Toast组件
 */

// Toast状态管理
const toastState = ref({
  visible: false,
  message: '',
  type: 'none' as 'success' | 'error' | 'loading' | 'none',
  duration: 1500
})

/**
 * 显示Toast
 * @param options Toast配置选项
 */
export const showToast = (options: {
  title: string
  icon?: 'success' | 'error' | 'loading' | 'none'
  duration?: number
}) => {
  const { title, icon = 'none', duration = 1500 } = options
  
  toastState.value = {
    visible: true,
    message: title,
    type: icon,
    duration
  }
}

/**
 * 隐藏Toast
 */
export const hideToast = () => {
  toastState.value.visible = false
}

/**
 * 显示成功Toast
 * @param title 提示内容
 * @param duration 显示时长
 */
export const showSuccessToast = (title: string, duration: number = 1500) => {
  showToast({
    title,
    icon: 'success',
    duration
  })
}

/**
 * 显示错误Toast
 * @param title 提示内容
 * @param duration 显示时长
 */
export const showErrorToast = (title: string, duration: number = 1500) => {
  showToast({
    title,
    icon: 'error',
    duration
  })
}

/**
 * 显示加载Toast
 * @param title 提示内容
 */
export const showLoadingToast = (title: string = '加载中...') => {
  showToast({
    title,
    icon: 'loading',
    duration: 0 // 加载状态不自动关闭
  })
}

/**
 * 获取Toast状态
 */
export const getToastState = () => toastState

/**
 * 监听Toast关闭事件
 */
export const onToastClose = (callback: () => void) => {
  // 这里可以实现监听逻辑
  return callback
} 